package uk.co.richardwkirk.parkrun.android.events.displays;

import java.util.ArrayList;

import uk.co.richardwkirk.parkrun.android.R;
import uk.co.richardwkirk.parkrun.android.events.distance.EventWithDistance;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ClosestEventExpandableListAdapter extends BaseExpandableListAdapter {

	Resources resources;
	ArrayList<EventWithDistance> eventsWithDistances;
	Context context;

	public ClosestEventExpandableListAdapter(Context ctxt, ArrayList<EventWithDistance> list) {
		resources = ctxt.getResources();
		this.eventsWithDistances = list;
		this.context = ctxt;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		EventWithDistance event = eventsWithDistances.get(groupPosition);
		return event.getDetails().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

		String detail = (String) getChild(groupPosition, childPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.event_selection_row, null);
		}

		TextView sequence = (TextView) convertView.findViewById(R.id.event_name);
		sequence.setText(detail);
		
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return eventsWithDistances.get(groupPosition).getDetails().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return eventsWithDistances.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return eventsWithDistances.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		EventWithDistance event = (EventWithDistance) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inf.inflate(R.layout.event_selection_group, null);
		}

		TextView heading = (TextView) convertView.findViewById(R.id.heading);
		if (event.isVisited()) {
			heading.setTextColor(resources.getColor(R.color.parkrun_brown));
		}
		else {
			heading.setTextColor(resources.getColor(R.color.parkrun_light_brown));
		}
		
		heading.setText(event.getEventName() + " (" + event.getDistanceString() + ")");
		
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
